﻿@using eLoan.Models;
@model GenerateNewScheduleModel

@{
    ViewBag.Title = "View Schedule";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>View Schedule</h2>
        <ol class="breadcrumb">
            <li>
                Funding
            </li>
            <li>
                <a href="@Url.Action("ReleaseToCustomer", "Funding")">Release to Customer</a>
            </li>
            <li class="active">
                <strong>View Schedule</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>
@using (Html.BeginForm("CreateNewPaymentSchedule", "LoanSchedule", FormMethod.Post, new { id = "saveForm" }))
{
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div id="divPaymentSchedule" class="col-lg-7">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <div class="pull-left">
                        <h3>@Model.DocumentNo</h3>
                        @Html.HiddenFor(m => Model.DocumentNo)
                        @Html.HiddenFor(m => Model.Source)
                    </div>
                    <div class="pull-right">
                        @if (Model.Source == "ReleaseToBranch")
                        {
                            @Html.ActionLink("Back to Release to Branch", "ReleaseToBranch", "Funding")
                        }
                        else
                        {
                            @Html.ActionLink("Back to Release to Customer", "ReleaseToCustomer", "Funding")
                        }
                    </div>
                </div>
                <div class="ibox-title">
                    <table class="table table-bordered table-striped">
                        <tr>
                            <td>@Html.LabelFor(m => Model.ProceedTypeID)</td>
                            <td>@Html.LabelFor(m => Model.Term)</td>
                            <td>@Html.LabelFor(m => Model.NewPaymentStartDate)</td>
                            @if (Model.ProceedTypeID == (int)AdministrationHelper.ProceedType.SemiMonthly)
                            {
                                <td>@Html.LabelFor(m => m.PaymentSecondDate)</td>
                            }
                        </tr>
                        <tr>
                            <td>
                                @Html.DisplayFor(m => m.ProceedTypeName)
                                @Html.HiddenFor(m => m.ProceedTypeID)
                                @Html.HiddenFor(m => m.ProceedTypeName)
                            </td>
                            <td>
                                @Html.DisplayFor(m => Model.Term)
                                @Html.HiddenFor(m => Model.Term, new { @id = "txtTerm" })
                            </td>
                            <td>
                                @if (Model.IsAdmin)
                                {
                                    <div class="form-group" id="psd">
                                        <div class="input-group date">
                                            <span class="input-group-addon">
                                                <i class="fa fa-calendar"></i>
                                            </span>
                                            @Html.TextBoxFor(m => m.NewPaymentStartDate, "{0:MM/dd/yyyy}", new { @id = "txtPaymentStartDate", @class = "form-control single-line" })
                                        </div>
                                    </div>
                                }
                                else
                                {
                                    @Html.ValueFor(m => m.NewPaymentStartDate, "{0:MM/dd/yyyy}")
                                    @Html.HiddenFor(m => m.NewPaymentStartDate)
                                }
                            </td>
                            @if (Model.ProceedTypeID == (int)AdministrationHelper.ProceedType.SemiMonthly)
                            {
                            <td>
                                @Html.TextBoxFor(m => m.PaymentSecondDate, new { @class= "single-line" })
                                @Html.HiddenFor(m => m.PaymentSecondDate)
                            </td>
                            }
                        </tr>
                    </table>
                </div>
                <div class="ibox-title">
                    <div>
                        <button type="submit" class="btn btn-primary" name="command" value="CreateSchedule" >Generate payment schedule</button>
                        <button type="submit" class="btn btn-primary" name="command" value="PrintSchedule">Print schedule</button>
                        <button type="submit" class="btn btn-primary" name="command" value="Save">Save</button>
                    </div>
                    @Html.HiddenFor(m => Model.DocumentDate)
                    @Html.HiddenFor(m => Model.Term)
                    @Html.HiddenFor(m => Model.OldPaymentStartDate)
                    @Html.HiddenFor(m => Model.OldPaymentEndDate)
                    @Html.HiddenFor(m => m.NewPaymentStartDate)
                    @Html.HiddenFor(m => m.NewPaymentEndDate)
                </div>
                <div class="ibox-content">
                    <div class="divScroll">
                        <table class="table table-bordered">
                            <thead class="k-grid-header">
                                <tr>
                                    <th></th>
                                    <th><span>Due Date</span></th>
                                    <th>Day of Week</th>
                                    <th>Cheque Details</th>
                                    <th><span>Due Amount</span></th>
                                </tr>
                            </thead>
                            <tbody>
                                @for (var i = 0; i < Model.Schedule.Count(); i++)
                                {
                                    var item = i + 1;
                                    <tr>
                                        <td>@item</td>
                                        <td>@Html.ValueFor(m => Model.Schedule[i].DueDate, "{0:MMM/dd/yyyy}")</td>
                                        <td>@Html.ValueFor(m => Model.Schedule[i].DueDate.DayOfWeek)</td>
                                        <td>@Html.TextBoxFor(m => Model.Schedule[i].ChequeDetails, new { @class="single-line" })</td>
                                        <td>
                                            @Html.ValueFor(m => Model.Schedule[i].DueAmount, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => Model.Schedule[i].ChargeTypeID)
                                            @Html.HiddenFor(m => Model.Schedule[i].DocumentNo)
                                            @Html.HiddenFor(m => Model.Schedule[i].DueAmount)
                                            @Html.HiddenFor(m => Model.Schedule[i].DueDate)
                                            @Html.HiddenFor(m => Model.Schedule[i].ID)
                                            @Html.HiddenFor(m => Model.Schedule[i].OtherCharges)
                                            @Html.HiddenFor(m => Model.Schedule[i].ScheduledAmount)
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
    
}

@section Styles {
    @Styles.Render("~/plugins/dataPickerStyles")
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/plugins/dataPicker")
    

<script id="onDocumentReady" type="text/javascript">

    $(document).ready(function () {

        $('#psd .input-group.date').datepicker({
            todayBtn: "linked",
            keyboardNavigation: false,
            forceParse: false,
            calendarWeeks: true,
            format: 'mm/dd/yyyy',
            autoclose: true,
            daysOfWeekDisabled: [0]

        });


        //$('#ped .input-group.date').datepicker({
        //    todayBtn: "linked",
        //    keyboardNavigation: false,
        //    forceParse: false,
        //    calendarWeeks: true,
        //    format: 'mm/dd/yyyy',
        //    autoclose: true

        //});


        $('#psd .input-group.date').datepicker()
            .on('changeDate', function (e) {
                SetPaymentDays();
            });

        //$('#ped .input-group.date').datepicker()
        //    .on('changeDate', function (e) {
        //        SetPaymentDays();
        //    });

        $(window).keydown(function (event) {
            if (event.keyCode == 13) {
                event.preventDefault();
                return false;
            }
        });

    });

    function SetPaymentDays() {
        CheckTerm();
        CheckPaymentStartDate();

        var term = $('#txtTerm').val().trim();
        var sd = $('#psd .input-group.date').datepicker('getDate');
        var date = new Date(sd);

        date.setDate(date.getDate() + parseInt(term) - 1);

        //$('#ped .input-group.date').datepicker("update", new Date(date));
    }

    function CheckTerm() {
        var div = $('#txtTerm').val().trim();

        if (div == '' || isNaN(div)) {
            $('#txtTerm').val('1');
        }
    }

    function CheckPaymentStartDate()
    {

        var sd = $('#psd .input-group.date').datepicker('getDate');
        //var docDate = $('#hdnDocumentDate').val();
        var now = new Date();

        if (isNaN(Date.parse(sd))) {
            $('#psd .input-group.date').datepicker("update", now);
        }

        //if (Date.parse(sd) < now) {
        //    $('#psd .input-group.date').datepicker("update", now);
        //}

    }

</script>

<style>
    .divScroll {
        height: 400px;
        overflow-x: scroll;
        border: solid 1px #EBEBEB;
    }
</style>

}